package com.china08.yxyapi.api.home.kecheng.topic;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;

/**
 * 专题课程--详情页--推荐课程
 * 
 * 推荐同贡献者的2个课程，如没有同贡献者的就推荐同分类下的2个课程
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengTopicParticularRecommendSrhController
        extends YxyApiBaseController {

    // 课程
    @Autowired
    private YxyLessonsTopicLessonRepository yxyLessonsTopicLessonRepository;

    // 分类
    @Autowired
    private YxyLessonsTopicCatalogRepository yxyLessonsTopicCatalogRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/topic/particular/recommend/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(HttpServletRequest request,
            HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String lessonId = reqModel.lessonId;
        List<RespModel> retuLst = new ArrayList<>();

        // 同贡献者下的两个课程
        YxyLessonsTopicLesson yxyLessonsTopicLessons = yxyLessonsTopicLessonRepository
                .findById(lessonId);
//        String aaName = yxyLessonsTopicLessons.getAuthor();
        // List<YxyLessonsTopicLesson> yxyLessonsTopicLessonLst =
        // yxyLessonsTopicLessonRepository
        // .findTop2ByAuthorAndStatusAndIsVisibleOrderByVvDesc(aaName, 2,
        // 1);
        // if (yxyLessonsTopicLessonLst.size() >= 2) {
        // for (YxyLessonsTopicLesson yxyLessonsTopicLesson :
        // yxyLessonsTopicLessonLst) {
        // RespModel model = new RespModel();
        // model.lessonId = yxyLessonsTopicLesson.getId();
        // model.vip = yxyLessonsTopicLesson.getVip();
        // model.cover = yxyLessonsTopicLesson.getCover();
        // model.count = yxyLessonsTopicLesson.getCount();
        // model.duration = yxyLessonsTopicLesson.getDuration();
        // model.name = yxyLessonsTopicLesson.getName();
        // model.author = yxyLessonsTopicLesson.getAuthor();
        // model.vv = yxyLessonsTopicLesson.getVv();
        //
        // YxyLessonsTopicCatalog yxyLessonsTopicCatalogs =
        // yxyLessonsTopicCatalogRepository
        // .findByid(yxyLessonsTopicLesson.getCatalogId3());
        // model.threeName = yxyLessonsTopicCatalogs.getName();
        // retuLst.add(model);
        // }
        // }
        // 同分类下的两个课程
        // else {
        List<YxyLessonsTopicLesson> yxyLessonsTopicLessonLst1 = yxyLessonsTopicLessonRepository
                .findTop2ByCatalogId3AndStatusAndIsVisibleOrderByVvDesc(
                        yxyLessonsTopicLessons.getCatalogId3(), 2, 1);
        if (yxyLessonsTopicLessonLst1.size() >= 2) {
            for (YxyLessonsTopicLesson yxyLessonsTopicLesson : yxyLessonsTopicLessonLst1) {
                RespModel model = new RespModel();
                model.lessonId = yxyLessonsTopicLesson.getId();
                model.vip = yxyLessonsTopicLesson.getVip();
                model.cover = yxyLessonsTopicLesson.getCover();
                model.count = yxyLessonsTopicLesson.getCount();
                model.duration = yxyLessonsTopicLesson.getDuration();
                model.name = yxyLessonsTopicLesson.getName();
                model.author = yxyLessonsTopicLesson.getAuthor();
                model.vv = yxyLessonsTopicLesson.getVv();

                YxyLessonsTopicCatalog yxyLessonsTopicCatalogs = yxyLessonsTopicCatalogRepository
                        .findByid(yxyLessonsTopicLesson.getCatalogId3());
                model.threeName = yxyLessonsTopicCatalogs.getName();
                retuLst.add(model);
            }
        }
        // }

        return retuLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程ID
        public String lessonId;

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // ##
        // 三级分类名称
        public String threeName;

        // ##
        // vip
        public Integer vip;
        // 封面
        public String cover;
        // 总课时
        public Integer count;
        // 总时长
        public Integer duration;
        // 课程名称
        public String name;
        // 贡献者
        public String author;
        // 学习人数
        public Integer vv;
        // 课程Id
        public String lessonId;
        // 上传者ID
        public String userId;

    }
}
